package cn.kgc.springcloud.service.impl;

import cn.kgc.springcloud.client.CityClient;
import cn.kgc.springcloud.client.ClazzClient;
import cn.kgc.springcloud.client.GroupClient;
import cn.kgc.springcloud.client.TagClient;
import cn.kgc.springcloud.dto.StudentDTO;
import cn.kgc.springcloud.entity.*;
import cn.kgc.springcloud.mapper.StudentMapper;
import cn.kgc.springcloud.mapper.StudentTagMapper;
import cn.kgc.springcloud.service.StudentService;
import cn.kgc.springcloud.util.DateUtil;
import cn.kgc.springcloud.vo.StudentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/8/31
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentTagMapper studentTagMapper;
    @Autowired
    private ClazzClient clazzClient;
    @Autowired
    private GroupClient groupClient;
    @Autowired
    private CityClient cityClient;
    @Autowired
    private TagClient tagClient;

    public List<StudentVO> selectAll(){
        ArrayList<StudentVO> studentVOS = new ArrayList<>();
        //查询学生的基本信息
        List<Student> students = studentMapper.selectAll();

        // 遍历集合students   Student - > StudentVO
        students.forEach(student -> {

            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student, studentVO);

            // 1. 根clazzId查询班级信息
            Integer clazzId = student.getClazzId();
            Clazz clazz = clazzClient.getById(clazzId);
            studentVO.setClazz(clazz);

            // 2.根据小组id 查询小组信息
            Integer groupId = student.getGroupId();
            Group group = groupClient.getById(groupId);
            studentVO.setGroup(group);

            // 3.根据城市id  查询城市信息
            Integer cityId = student.getCityId();
            City city = cityClient.getById(cityId);
            studentVO.setCity(city);

            // 4.根据学生id 查询标签id的集合
            Integer stuId = student.getId();
            List<StudentTag> studentTags = studentTagMapper.selectBySid(stuId);
            // 5.遍历studentTags标签集合 查询标签信息
            ArrayList<Tag> tags = new ArrayList<>();
            studentTags.forEach(studentTag -> {
                Integer tid = studentTag.getTid();
                // 根据tid 调用标签服务  查询标签信息
                Tag tag = tagClient.findTagById(tid);
                tags.add(tag);
            }) ;

            studentVO.setTags(tags);
            studentVOS.add(studentVO);
        }) ;

        return studentVOS;
    }

    @Override
    public StudentVO addStudent(StudentDTO studentDTO) {
        // 1.获取学生的基本信息
        Student student = new Student();
        BeanUtils.copyProperties(studentDTO, student);

        // 根据学生的生日计算学生的年龄 属相 星座
        String starts = DateUtil.getConstellation(student.getBir());
        String attr = DateUtil.getYear(student.getBir());
        int age = DateUtil.getAge(student.getBir());

        student.setStarts(starts);
        student.setAge(age);
        student.setAttr(attr);

        //将学生信息插入学生表
       Integer line = studentMapper.addStudent(student);

         //获取学生自增id
        Integer sid = student.getId();

        // 2.获取学生的标签列表  将标签列表中的数据插入 student_tag表
        List<Integer> tagIds = studentDTO.getTagIds();
        tagIds.forEach(tid->{
            studentTagMapper.addStudentTag(new StudentTag(sid,tid));
        }) ;

        // 3.将插入的学生信息 转换为 studentvo类型的实例返回
        StudentVO studentVO = new StudentVO();
        BeanUtils.copyProperties(student, studentVO);

        // 根据clazzId查询班级信息
        Clazz clazz = clazzClient.getById(student.getClazzId());
        studentVO.setClazz(clazz);

        // 根据groupId查询小组信息
        Group group = groupClient.getById(student.getGroupId());
        studentVO.setGroup(group);
        // 根据城市id 查询城市信息
        City city = cityClient.getById(student.getCityId());
        studentVO.setCity(city);
        //根据标签id 查询标签信息
        ArrayList<Tag> tags = new ArrayList<>();
        tagIds.forEach(tagid->{
            Tag tag = tagClient.findTagById(tagid);
            tags.add(tag);
        });
        studentVO.setTags(tags);

        return studentVO;
    }

}
